package com.gliese.bambook.xpath;

import java.util.Iterator;
import java.util.List;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.junit.Test;

public class DeployFileLoaderSample {

	private org.dom4j.Document doc;

	@Test
	public void testDeployFileLoaderSmaple() {
		browseRootChildren();
	}

	public void browseRootChildren() {

		/*
		 * Let's look how many "James" are in our XML Document an iterate them (
		 * Yes, there are three James in this project ;) )
		 */

		XPath xpathSelector = DocumentHelper
				.createXPath("/people/person[@name='James']");
		List results = xpathSelector.selectNodes(doc);
		for (Iterator iter = results.iterator(); iter.hasNext();) {
			Element element = (Element) iter.next();
			System.out.println(element.getName());
		}

		// select all children of address element having person element with
		// attribute and value "Toby" as parent
		String address = doc.valueOf("//person[@name='Toby']/address");

		// Bob's hobby
		String hobby = doc.valueOf("//person[@name='Bob']/hobby/@name");

		// the second person living in UK
		String name = doc.valueOf("/people[@country='UK']/person[2]");

		// select people elements which have location attriute with the value
		// "London"
		Number count = doc.numberValueOf("//people[@location='London']");

	}

}
